package com.sanjiahantang.youjiarole.module.common.models.impl;

import android.content.Context;
import android.text.TextUtils;
import android.widget.TextView;

import com.sanjiahantang.youjiarole.base.CallBack;
import com.sanjiahantang.youjiarole.config.ConfigValue;
import com.sanjiahantang.youjiarole.config.SPConfig;
import com.sanjiahantang.youjiarole.config.StaticValue;
import com.sanjiahantang.youjiarole.http.BaseDelegate;
import com.sanjiahantang.youjiarole.http.OkHttpClientManager;
import com.sanjiahantang.youjiarole.module.common.beans.HomeBean;
import com.sanjiahantang.youjiarole.module.common.beans.PersonBean;
import com.sanjiahantang.youjiarole.module.common.models.IHomeModel;
import com.sanjiahantang.youjiarole.module.common.views.HomeView;
import com.sanjiahantang.youjiarole.module.login.views.impl.LoginActivity;
import com.sanjiahantang.youjiarole.module.match.beans.MatchListBean;
import com.sanjiahantang.youjiarole.utils.SPUtils;
import com.squareup.okhttp.Request;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by zy on 2017/9/1.
 */

public class HomeModel implements IHomeModel ,StaticValue{


    @Override
    public void getHomePageData(final Context context,final CallBack<HomeBean> callBack) {
        /*if(ConfigValue.isDebug){


            new Handler().postDelayed(new Runnable() {
                public void run() {
                    HomeBean model = null;
                    String json = logJson();
                    Log.e("aaa",json);
                    model = new Gson().fromJson(json,HomeBean.class);
                    callBack.onSuccess(model,context);
                }
            }, 1000);
            return;
        }*/
        indexRole(context,callBack);
//        if (!TextUtils.isEmpty(ConfigValue.city)){
//
//        }else{
//            Map<String,String> params = new HashMap<>();
//            params.put(KEY_TOKEN, ConfigValue.DATA_KEY);
//            params.put(KEY_ROLE_ID, ""+SPUtils.get(context,SPConfig.ROLE_ID,""));
//            params.put(KEY_PHONE,SPUtils.get(context,SPConfig.PHONE,"").toString());
//            loadUserInfo(context, params, new CallBack<PersonBean>() {
//                @Override
//                public void onSuccess(PersonBean response, final Context context, Object tag) {
//
//                    if (response != null && response.getCode() == RESPONSE_CODE_SUC){
//                        String address = response.getData().getAddress();
//                        if (address != null){
//                            SPUtils.put(context,SPConfig.ADDRESS,address == null ? "" : address);
//                            String[] addressArr = address.split(" ");
//                            if (addressArr.length >= 3){
//                                ConfigValue.city = addressArr[1];
//                            }else {
//                                ConfigValue.city = addressArr[0];
//                            }
//                        }else{
//                            ConfigValue.city = DEFAULT_CITY;
//                        }
//                    }else {
//                        ConfigValue.city = DEFAULT_CITY;
//                    }
//
//                    indexRole(context,callBack);
//                }
//
//                @Override
//                public void onFailure(String message) {
//                    callBack.onFailure(message);
//                }
//            });
//
//        }


    }

    private void indexRole(final Context context,final CallBack<HomeBean> callBack){
        String token = ConfigValue.DATA_KEY;
        String phone = SPUtils.get(context, SPConfig.PHONE,"").toString();
        if (TextUtils.isEmpty(token) || TextUtils.isEmpty(phone)){
            LoginActivity.logout(context);
            return;
        }
        Map<String,String> param = new HashMap<>();
        param.put(KEY_TOKEN,token);
        param.put(KEY_PHONE,phone);
//        param.put("address",ConfigValue.city);
        OkHttpClientManager.postAsyn(context, ConfigValue.API_URL + "/index/customer/indexRole",param,new BaseDelegate.ResultCallback<HomeBean>() {

            @Override
            public void onError(Request request, Object tag, Exception e) {
                callBack.onFailure(e.getMessage());
            }

            @Override
            public void onResponse(HomeBean response, Object tag) {
                callBack.onSuccess(response,context, null);
            }
        });
    }

    public void loadUserInfo(final Context context, Map<String, String> params, final CallBack<PersonBean> callBack) {
        OkHttpClientManager.postAsyn(context, ConfigValue.API_URL + "/user/phone/personal/searchPersonalInfoForRole", params,
                new BaseDelegate.ResultCallback<PersonBean>() {
                    @Override
                    public void onError(Request request, Object tag, Exception e) {
                        callBack.onFailure(e.getMessage());
                    }

                    @Override
                    public void onResponse(PersonBean response, Object tag) {
                        callBack.onSuccess(response, context, tag);
                    }
                }, true);
    }

    @Override
    public void getMoreHomePageData(final Context context, Map<String,String> param, final CallBack<MatchListBean> callBack) {

    }
}
